package cn.dnui_cjy529.dnuifood_16110100529.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_cjy529.dnuifood_16110100529.bean.FoodBean;
import cn.dnui_cjy529.dnuifood_16110100529.controller.FoodListActivity;
import cn.dnui_cjy529.dnuifood_16110100529.model.UserModel;

public class FoodListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<FoodBean> mDataList;
    private LayoutInflater mLayoutInflater;
    //private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public FoodListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.activity_food_list_adapter,parent,false);
        // v.setOnClickListener((View.OnClickListener) this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final FoodBean entity = (FoodBean) mDataList.get(position);
        if (null == entity)
            return;
        FoodListAdapter.ViewHolder viewHolder = (FoodListAdapter.ViewHolder) holder;
        viewHolder.foodname.setText(entity.getFoodname());
        viewHolder.price.setText(entity.getPrice()+"元");
        viewHolder.intro.setText(entity.getIntro());
        //  viewHolder.image.setImageResource(Integer.parseInt(entity.getPic()));

        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(viewHolder.pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(mDataList.get(position).getFood_id());
                String str1 = String.valueOf(mDataList.get(position).getFoodname());
                Intent intent = new Intent(mContext,FoodListActivity.class);
                //intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("food_id",str);
                intent.putExtra("foodname",str1);
                mContext.startActivity(intent);
            }
        });

    }



    @Override
    public int getItemCount() {
        if (mDataList==null)
            return 0;
        else
            return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageButton collect_1;
        TextView foodname,intro,price;
        ImageView pic;
        LinearLayout food_container;
        public ViewHolder(View view) {
            super(view);
            foodname=(TextView) view.findViewById(R.id.collect_name);
            intro=(TextView) view.findViewById(R.id.food_intro);
            pic=(ImageView)view.findViewById(R.id.food_pic1);
            price=(TextView) view.findViewById(R.id.price);
            collect_1=(ImageButton)view.findViewById(R.id.collect_1);
            food_container=(LinearLayout)itemView.findViewById(R.id.ll_food_list);
        }
    }
}